package com.judian.smart.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil {

    //日期格式转星期格式
    public static String dateToWeek(Date date) {
        String[] weekOfDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekOfDays[w];
    }

    //字符串转日期
    public static Date stringToDate(String time) {
        //2019-06-28T08:00:00
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
        Date date = new Date();
        try {
            date = sdf.parse(time);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
            return date;
        }
    }

    //时间戳转换几秒后、几个小时后、几天后 时间单位
    public static String tampToUnit(long time) {
        long s = time / 1000; //转换成秒数级
        long min = 60; //  1分钟 = 60 秒
        long hour = 60 * 60;  // 1小时 = 3600秒
        long day = 60 * 60 * 24;  // 1天 = 3600 * 24
        if (s >= 0 && s < min) {
            return s + "秒";
        } else if (s >= min && s < hour) {
            long m = s / min; //秒转分钟
            long yu = s % min; //取余
            if (yu == 0) {
                return m + "分钟";
            } else {
                return m + "分钟" + yu + "秒";
            }
        } else if (s >= hour && s < day) {
            long h = s / hour; //秒转小时
            long yu = s % hour; //取余
            if (yu < min) {
                return h + "小时";
            } else {
                return h + "小时" + yu / min + "分钟";
            }
        } else if (s >= day) {
            long h = s / (day); //秒转天数
            long yu = s % day;  //取余
            if (yu < hour) {
                return h + "天";
            } else {
                return h + "天" + yu / hour + "小时";
            }
        }
        return "";
    }

}
